/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2emu.gameserver.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;

import com.l2emu.Config;
import com.l2emu.Server;
import com.l2emu.gameserver.L2GameServer;
import com.l2emu.status.Status;
import com.l2emu.util.Version;

/**
 * @author Rayan
 */

public final class BootManager
{
	private static Status _statusServer;
	private static final Version	version	= new Version();
	
	/**
	 * Calls Boot Preparator.
	 * 
	 * @param args
	 * @throws Throwable 
	 */
	public static void main(String[] args) throws Throwable
	{
		new BootManager();
	}

	public BootManager() throws Throwable
	{	
		Server.serverMode = Server.MODE_GAMESERVER;
		
		Util.printSection("Boot Manager");

		//inits logging
		// ------------
		System.out.println("BootManager: Initializing Logging.");
		initLogging();

		// create all needed files and dirs
		// --------------------------------
		System.out.println("BootManager: Creating Boot Folders and Files.");
		createBootDirs();

		//reads Config b4 init gameserver boot
		// --------------------------------------------
		System.out.println("BootManager: Initializing Configs.");
		Config.loadAll();

		// need to re print config just loaded.
		// ------------------------------------
		Util.printSection("Boot Manager");
		System.out.println("BootManager: Config Sucessffully Loaded.");

		// telnet
		Util.printSection("Telnet");
		loadTelnet();
		
		System.out.println("BootManager: Preparations Done. Staring GameServer!");
		new L2GameServer();
	}

	private void loadTelnet() throws Exception
	{
		if (Config.IS_TELNET_ENABLED)
		{
			_statusServer = new Status(Server.serverMode);
			_statusServer.start();
		}
		else
			System.out.println("Telnet server is currently disabled.");	
	}
	
	private void createBootDirs() throws IOException
	{
		// Create logging Directories
		// -----------------------------
		File logFolder = new File(Config.DATAPACK_ROOT, "log");
		File logFolderGame = new File(logFolder, "game");
		logFolderGame.mkdir();
		logFolder.mkdir();
		
		
		new File("log/java").mkdirs(); 
		new File("log/error").mkdirs(); 
		
		new File("log/audit").mkdirs(); 
		new File("log/chat").mkdirs(); 
		new File("log/item").mkdirs(); 
		
		new File("log/GMAudit").mkdirs(); 
		new File("log/admin-commands").mkdirs(); 

		// Create folders for datapck
		//------------------------------
		new File(Config.DATAPACK_ROOT, "data/crests").mkdirs();

		// define where version file will be located in
		// --------------------------------------------
		File buildFile = new File("./config/versionning/build-version.properties");

		// delete any previous version
		// ---------------------------
		if (buildFile.exists())
			buildFile.delete();

		// since version file got deleted, now we create new for make sure it is the correct version in each boot.
		// ------------------------------------------------------------------------------------------------------
		buildFile.createNewFile();

		// now we transfer manifest.mf info from our jar to build-version.properties file.
		// so can be correctly loaded by Config
		// -----------------------------------
		version.transferInfo("l2emu-game-" + Version.STABLE_RELEASE + ".jar", "gameserver", buildFile);

		System.out.println("BootManager: All Directories and Files Created!");
	}

	private void initLogging()
	{
		// Initialize logging, // must be loaded b4 first log output
		// -------------------------------------------------------------------------
		try
		{
			
			// Local Constants
			final String LOG_FOLDER = "log"; // Name of folder for log file
			final String LOG_NAME = "./config/logging.cfg"; // Name of log file
			
			/*** Main ***/
			// Create log folder
			File logFolder = new File(Config.DATAPACK_ROOT, LOG_FOLDER);
			logFolder.mkdir();
			
			// Create input stream for log file -- or store file data into memory
			InputStream is = new FileInputStream(new File(LOG_NAME));
			LogManager.getLogManager().readConfiguration(is);
			is.close();

			System.out.println("BootManager: Logging Initialized.");
		}
		catch (Exception e)
		{
			throw new Error("Failed to Load " + "./config/logging.cfg" + " File.");
		}
	}
}